using MicroMsg.Common.Utils;
using MicroMsg.Manager;
using MicroMsg.Resource.@string;
using MicroMsg.Storage;
using MicroMsg.UI.Page;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Media;
using System.Windows.Media.Imaging;
namespace MicroMsg.UI.UserContrl
{
	public class ContactListItem : UserControl
	{
		public delegate void ListItemClick(object sender, object object_0);
		public delegate void ListItemCustomize(object sender, object object_0);
		private const string TAG = "ContactListItem";
		public static Dictionary<string, Contact> mDictContactChoosen = new Dictionary<string, Contact>();
		public static List<string> sListContactsDisable;
		private static Dictionary<int, ContactListItem> sDictItemsInView = new Dictionary<int, ContactListItem>();
		private static int sNCurID = 0;
		public static bool sBIsScrolling = false;
		private int mNID;
		public ContactsViewType mConItemType;
		private bool mBMouseDownHere;
		public static DependencyProperty ListItemProperty = DependencyProperty.Register("ItemObject", typeof(Contact), typeof(ContactListItem), new PropertyMetadata(null, new PropertyChangedCallback(ContactListItem.OnItemPropertyChanged)));
		private bool mBIsLoaded;
		public static long sLTimeComsume = 0L;
		public static int sNCount = 0;
		private object mDObjMenuContext;
		private bool mBIsBindedVisibility;
		internal Border borderImgHead;
		internal Image ImgHead;
		internal Image imgVerifyTag;
		internal EmojiText txBkTitle;
		internal Image imgWeiboTag;
		internal EmojiText txBkSignature;
		internal CheckBox checkBoxChoosen;
		internal Button ChatEntryLayout;
		internal Image ChatEntry;
		private bool _contentLoaded;
		public event ContactListItem.ListItemClick ItemClick;
		public event ContactListItem.ListItemCustomize ItemNeedCustomize;
		public Contact ItemObject
		{
			get
			{
				return (Contact)base.GetValue(ContactListItem.ListItemProperty);
			}
			set
			{
				base.SetValue(ContactListItem.ListItemProperty, value);
			}
		}
		private static void OnItemPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == ContactListItem.ListItemProperty)
			{
				(dependencyObject_0 as ContactListItem).OnListItemPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		public ContactListItem()
		{
			ShowObjectInMemory.AddOneObjectRecord(this);
			this.InitializeComponent();
			this.txBkTitle.Foreground = R.theme.LISTB_NAME_WORD_BRUSH;
			this.txBkSignature.Foreground = R.theme.LISTB_SIGNATURE_WORD_BRUSH;
			this.borderImgHead.set_Background(R.theme.COMMON_BRUSH);
			this.checkBoxChoosen.set_Visibility(1);
			this.mBMouseDownHere = false;
			this.mNID = ContactListItem.sNCurID++;
			base.add_Loaded(new RoutedEventHandler(this.ContactListItem_Loaded));
			base.add_Unloaded(new RoutedEventHandler(this.ContactListItem_Unloaded));
		}
		~ContactListItem()
		{
			ShowObjectInMemory.RemoveOneObjectRecord(this);
		}
		private void ContactListItem_Loaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = true;
			if (!ContactListItem.sDictItemsInView.ContainsKey(this.mNID))
			{
				ContactListItem.sDictItemsInView.Add(this.mNID, this);
			}
		}
		private void ContactListItem_Unloaded(object sender, RoutedEventArgs e)
		{
			this.mBIsLoaded = false;
			if (ContactListItem.sDictItemsInView.ContainsKey(this.mNID))
			{
				ContactListItem.sDictItemsInView.Remove(this.mNID);
			}
		}
		private void btnLayoutRoot_Click(object sender, object object_0)
		{
			if (this.ItemClick != null)
			{
				this.ItemClick(this, this.ItemObject);
			}
		}
		private void ChatEntry_Click(object sender, object object_0)
		{
			string text = (this.ItemObject != null) ? this.ItemObject.strUsrName : null;
			if (text != null)
			{
				ChatContainerPage.GoToChatPage(PageManager.getInstance().GetTopPage(), text, null, null, null);
			}
		}
		private void displayChatEntryIfNeed(Contact item)
		{
			if (this.ItemObject == null)
			{
				this.ChatEntryLayout.set_Visibility(1);
				return;
			}
			if (this.mConItemType != ContactsViewType.CONTACT_VIEW_CHECKTOADD && this.mConItemType != ContactsViewType.CONTACT_VIEW_CLICKTOADD && this.mConItemType != ContactsViewType.CONTACT_VIEW_GROUPONLY_CLIKTOADD && this.mConItemType != ContactsViewType.CONTACT_VIEW_OTHER)
			{
				if (this.mConItemType != ContactsViewType.CONTACT_VIEW_BLACKLIST)
				{
					if (ContactHelper.isNeedShowChatEntry(item))
					{
						this.ChatEntryLayout.set_Visibility(0);
						return;
					}
					this.ChatEntryLayout.set_Visibility(1);
					return;
				}
			}
			this.ChatEntryLayout.set_Visibility(1);
		}
		private void itemChecked(object sender, RoutedEventArgs e)
		{
			if (!ContactListItem.mDictContactChoosen.ContainsKey(this.ItemObject.strUsrName))
			{
				ContactListItem.mDictContactChoosen.Add(this.ItemObject.strUsrName, this.ItemObject);
				this.ItemClick(sender, e);
			}
		}
		private void itemUnChecked(object sender, RoutedEventArgs e)
		{
			if (ContactListItem.mDictContactChoosen.ContainsKey(this.ItemObject.strUsrName))
			{
				ContactListItem.mDictContactChoosen.Remove(this.ItemObject.strUsrName);
				this.ItemClick(sender, e);
			}
		}
		public void simulateCheckBoxHit()
		{
			if (this.checkBoxChoosen.get_Visibility() == null)
			{
				ToggleButton arg_42_0 = this.checkBoxChoosen;
				bool? isChecked = this.checkBoxChoosen.get_IsChecked();
				arg_42_0.set_IsChecked(isChecked.get_HasValue() ? new bool?(!isChecked.GetValueOrDefault()) : default(bool?));
			}
		}
		public static void RefreshHeadImgInView(string strUserToRefresh)
		{
			if (strUserToRefresh != null && strUserToRefresh.get_Length() != 0)
			{
				List<ContactListItem> list = Enumerable.ToList<ContactListItem>(ContactListItem.sDictItemsInView.get_Values());
				using (List<ContactListItem>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ContactListItem current = enumerator.get_Current();
						if (current != null && current.ItemObject != null && current.ItemObject.strUsrName != null && strUserToRefresh == current.ItemObject.strUsrName)
						{
							current.ImgHead.set_Source(HeadImgMgr.getHeadImage(current.ItemObject));
							ImageSource arg_85_0 = current.ImgHead.get_Source();
							BitmapImage arg_84_0 = HeadImgMgr.defaultHeadImage;
						}
					}
				}
				return;
			}
		}
		public static void RefreshItemInfo(string strUserToRefresh)
		{
			if (strUserToRefresh != null && strUserToRefresh.get_Length() != 0)
			{
				List<ContactListItem> list = Enumerable.ToList<ContactListItem>(ContactListItem.sDictItemsInView.get_Values());
				using (List<ContactListItem>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						ContactListItem current = enumerator.get_Current();
						if (current != null && current.ItemObject != null && current.ItemObject.strUsrName != null && strUserToRefresh == current.ItemObject.strUsrName)
						{
							current.refreshTextInfo(current.ItemObject);
						}
					}
				}
				return;
			}
		}
		public static void ShowInViewItemsImg()
		{
			List<ContactListItem> list = Enumerable.ToList<ContactListItem>(ContactListItem.sDictItemsInView.get_Values());
			int num = 0;
			using (List<ContactListItem>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ContactListItem current = enumerator.get_Current();
					if (current != null && current.ItemObject != null && current.ItemObject.strUsrName != null)
					{
						num++;
						current.ImgHead.set_Source(HeadImgMgr.getHeadImage(current.ItemObject));
					}
				}
			}
		}
		public static void ShowInViewItemsCachedImg()
		{
		}
		public static void CheckedAllItemInView()
		{
			List<ContactListItem> list = Enumerable.ToList<ContactListItem>(ContactListItem.sDictItemsInView.get_Values());
			using (List<ContactListItem>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ContactListItem current = enumerator.get_Current();
					if (current != null && current.ItemObject != null && current.ItemObject.strUsrName != null)
					{
						current.checkBoxChoosen.set_IsChecked(new bool?(true));
					}
				}
			}
		}
		public static void UnCheckAllItemInView()
		{
			List<ContactListItem> list = Enumerable.ToList<ContactListItem>(ContactListItem.sDictItemsInView.get_Values());
			using (List<ContactListItem>.Enumerator enumerator = list.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					ContactListItem current = enumerator.get_Current();
					if (current != null && current.ItemObject != null && current.ItemObject.strUsrName != null)
					{
						current.checkBoxChoosen.set_IsChecked(new bool?(false));
					}
				}
			}
		}
		private void OnListItemPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			this.RefreshItem(dependencyPropertyChangedEventArgs_0.get_NewValue() as Contact);
		}
		private void clearContextMenu()
		{
			this.mDObjMenuContext = base.GetValue(ContextMenuService.ContextMenuProperty);
			base.SetValue(ContextMenuService.ContextMenuProperty, null);
		}
		private void resumeContextMenu()
		{
			if (this.mDObjMenuContext != null)
			{
				base.SetValue(ContextMenuService.ContextMenuProperty, this.mDObjMenuContext);
				this.mDObjMenuContext = null;
			}
		}
		private void bindVisibility()
		{
			if (this.mBIsBindedVisibility)
			{
				return;
			}
			this.mBIsBindedVisibility = true;
			Binding binding = new Binding("Visibility");
			binding.set_Source(ContactsView.sCtrlForGroupHeaderVisiblility);
			base.SetBinding(UIElement.VisibilityProperty, binding);
		}
		private void RefreshItem(Contact item)
		{
			if (this.ItemNeedCustomize != null)
			{
				this.ItemNeedCustomize(this, null);
			}
			if (ContactHelper.isNeedShowMenuHeader(item))
			{
				this.resumeContextMenu();
			}
			else
			{
				this.clearContextMenu();
			}
			if (item == null)
			{
				return;
			}
			this.displayChatEntryIfNeed(item);
			this.refreshTWeiBo(item);
			this.refreshVerifyTag(item);
			new ImageSourceConverter();
			BitmapImage cacheHeadImage = HeadImgMgr.getCacheHeadImage(this.ItemObject);
			if (cacheHeadImage != this.ImgHead.get_Source())
			{
				this.ImgHead.set_Source(HeadImgMgr.getCacheHeadImage(this.ItemObject));
			}
			this.refreshTextInfo(item);
			if (ContactsViewType.CONTACT_VIEW_CHECKTOADD != this.mConItemType)
			{
				if (ContactsViewType.CONTACT_VIEW_CLICKTOADD == this.mConItemType)
				{
					if (ContactListItem.sListContactsDisable.Contains(this.ItemObject.strUsrName))
					{
						base.set_Visibility(1);
						return;
					}
					base.set_Visibility(0);
				}
				return;
			}
			if (ToolFunction.checkIsStrSystemPlugin(this.ItemObject.strUsrName) || AccountMgr.getCurAccount().strUsrName == this.ItemObject.strUsrName || (ContactListItem.sListContactsDisable != null && ContactListItem.sListContactsDisable.Contains(this.ItemObject.strUsrName)) || this.ItemObject.strUsrName.EndsWith("@chatroom"))
			{
				this.checkBoxChoosen.set_Visibility(1);
				return;
			}
			this.checkBoxChoosen.set_Visibility(0);
			if (ContactListItem.mDictContactChoosen.ContainsKey(this.ItemObject.strUsrName))
			{
				this.checkBoxChoosen.set_IsChecked(new bool?(true));
				return;
			}
			this.checkBoxChoosen.set_IsChecked(new bool?(false));
		}
		private void refreshTextInfo(Contact item)
		{
			if (item == null)
			{
				return;
			}
			this.txBkTitle.Text = ContactListItem.GetContactDisName(item);
			this.txBkSignature.Text = this.ItemObject.strSignature;
		}
		private void refreshVerifyTag(Contact item)
		{
			if (item != null && ContactHelper.IsVerifiedContactVerified(item))
			{
				if (this.imgVerifyTag.get_Source() == null)
				{
					this.imgVerifyTag.set_Source(ToolFunction.GetImgFromContentSource("/Resource/Img/ContactList/VerifyTag.png"));
				}
				this.imgVerifyTag.set_Visibility(0);
				return;
			}
			this.imgVerifyTag.set_Visibility(1);
		}
		private void refreshTWeiBo(Contact item)
		{
			this.imgWeiboTag.set_Visibility(1);
			this.txBkTitle.set_MaxWidth(311.0);
		}
		public static string GetContactDisName(Contact item)
		{
			if (item == null)
			{
				return "";
			}
			if (item.strUsrName == AccountMgr.strUsrName)
			{
				return strings.Float_Bottle_Me;
			}
			return ContactHelper.getDisplayName(item.strUsrName, item);
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Contact/ContactListItem.xaml", 2));
			this.borderImgHead = (Border)base.FindName("borderImgHead");
			this.ImgHead = (Image)base.FindName("ImgHead");
			this.imgVerifyTag = (Image)base.FindName("imgVerifyTag");
			this.txBkTitle = (EmojiText)base.FindName("txBkTitle");
			this.imgWeiboTag = (Image)base.FindName("imgWeiboTag");
			this.txBkSignature = (EmojiText)base.FindName("txBkSignature");
			this.checkBoxChoosen = (CheckBox)base.FindName("checkBoxChoosen");
			this.ChatEntryLayout = (Button)base.FindName("ChatEntryLayout");
			this.ChatEntry = (Image)base.FindName("ChatEntry");
		}
	}
}
